package com.qqpz.sign_project.controller;

import com.google.gson.Gson;
import com.qqpz.sign_project.entity.Courses;
import com.qqpz.sign_project.service.CoursesService;
import com.qqpz.sign_project.service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 吴刚
 * @project sign_project/com.qqpz.sign_project.controller
 * @time 2021/6/28 15:34
 */

@RestController
@RequestMapping("/v1/courses")
public class CoursesController {
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private RecordsService recordsService;
    //查询整表
    @GetMapping(path = "/")
    public String queryCoursesInfo(){
        List<Courses> courses = coursesService.queryCoursesInfo();
        Map<String,Object> map=new HashMap<>();
        if(courses!=null){
            map.put("result","success");
            map.put("courses",courses);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    //按教师姓名查询
    @GetMapping(path = "/teacher/{username}")
    public String queryByUserName(@PathVariable("username")String username){
        List<Courses> courses = coursesService.queryByUserName(username);
        Map<String,Object> map=new HashMap<>();
        if(courses!=null){
            map.put("result","success");
            map.put("courses",courses);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    //按id查询
    @GetMapping(path = "/id/{id}")
    public String queryById(@PathVariable("id")String id){
        Courses courses = coursesService.queryById(id);
        Map<String,Object> map=new HashMap<>();
        if(courses!=null){
            map.put("result","success");
            map.put("courses",courses);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    //按课程名模糊查找
    @GetMapping(path = "/{name}")
    public String queryByName(@PathVariable("name")String name){
        List<Courses> courses = coursesService.queryByName(name);
        Map<String,Object> map=new HashMap<>();
        if(courses!=null){
            map.put("result","success");
            map.put("courses",courses);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    //按课程名精确查找
    @GetMapping(path = "/{name}/accurate")
    public String queryByNameAccurate(@PathVariable("name")String name){
        List<Courses> courses = coursesService.queryByNameAccurate(name);
        Map<String,Object> map=new HashMap<>();
        if(courses!=null){
            map.put("result","success");
            map.put("courses",courses);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    //查找地址
    @GetMapping(path = "/address/{id}")
    public String queryAddress(@PathVariable("id")String id){
        String location = coursesService.queryAddress(id);
        Map<String,Object> map=new HashMap<>();
        if(location!=null){
            map.put("result","success");
            map.put("locationName",location);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    //新增
    @PostMapping(path ="/")
    public String insertCourses(@RequestBody Courses courses){
        courses.setId(UUID.randomUUID().toString());
        System.out.println(courses);
        int i = 0;
        try {
            i = coursesService.insertCourses(courses);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,String> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }

    }

    //改描述，课程时间,截止时间，签到状态,作业
    @PostMapping(path ="/update")
    public String updateCourses(@RequestBody Courses courses) {
        int i = 0;
        try {
            i = coursesService.updateCourses(courses);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,String> map = new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    //按id删除
    @PostMapping(path ="/delete")
    @Transactional(propagation= Propagation.REQUIRED,rollbackForClassName="Exception")
    public String deleteCoursesById(@RequestBody Courses courses){
        int i = 0;
        int j=0;
        try {
            i = coursesService.deleteCoursesById(courses.getId());
            j= recordsService.deleteRecordsByCourseId(courses.getId());
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
            j=0;
        }
        Map<String, String> map = new HashMap<>();
        if(i!=0&&j!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result"," fail");
            return new Gson().toJson(map);
        }
    }



}
